<template>
	<view class="fun-flex-col fun-justify-start fun-relative page">
		<view class="fun-flex-col">

			<text class="fun-self-start font text_3">Bind your Email Adress</text>
			<text class="text_4">Tie your email to withdraw and don't miss our important notifications!</text>
			
			<view class="fun-flex-col fun-items-start fun-self-stretch group">
				<text class="font_2 text_5">Your email address</text>
				<input class="email-input" v-model="postData.email" placeholder="Enter Your email address" type='text'/>
			</view>
			
			<view class="fun-flex-col fun-items-start fun-self-stretch group">
				<text class="font_2 text_5">Verification Code</text>
				<view class="send-wrap fun-flex-row fun-justify-between fun-items-end">
					<input class="email-input" v-model="postData.verificationCode" placeholder="Enter Verification Code" type='tel'/>
					<view @click="send">Send</view>
				</view>
			</view>
			
			<view class="tips">A verification code has been sent to your bound email address.</view>
			
			<view class="font_2 text-wrapper_2" @click="confirm">Confirm</view>
		</view>
		
		<!-- <view class="fun-flex-col fun-justify-start section_2 pos">
			<view class="fun-flex-col section_4">
				<image class="fun-self-center image_6"
					src="https://ide.code.fun/api/image?token=671e89b1c471750012dd23ac&name=42848a7df83d109c63a030b5a13937a1.png" />
				<view class="fun-flex-col fun-items-center fun-self-stretch mt-27">
					<text class="font text_6">Invalid email address</text>
					<text class="fun-mt-14 font_3 text_7">Please enter a valid email address</text>
				</view>
				<view class="fun-flex-col fun-justify-start fun-items-center fun-self-center text-wrapper mt-27">
					<text class="font_3 text_8">Done</text>
				</view>
			</view>
		</view> -->
		<common-modal v-if='isShowModal' :popData="popData" @done="isShowModal = false" @close='isShowModal = false'></common-modal>
	</view>
</template>

<script>
	import commonModal from "@/components/commonModal.vue";
	import {regEmail} from "@/common/js/func.js";
	import {captchaEmail,bindEmail} from "@/api/common.js"
	export default {
		data() {
			return {
				postData:{
					email:"",
					verificationId:'',
					verificationCode:""
				},
				isShowModal:false,
				popData:{}
			};
		},

		methods: {
			send(){
				if(!this.postData.email){
					this.UTILS.logStr("Enter Your email address")
					return
				}
				if(!regEmail(this.postData.email)){
					this.popData = {
						icon:require("@/static/common/tips.png"),
						title:"Invalid email address",
						content:"Please enter a valid email address",
						btnText:["Done"]
					}
					this.isShowModal = true;
					return
				}
				const params = {
					type:2,
					email:this.postData.email
				}
				captchaEmail(params).then(res => {
					if(res.code == 0){
						this.postData.verificationId = res.data?.verificationId || "";
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			},
			confirm(){
				if(!this.postData.verificationCode){
					this.UTILS.logStr("Enter Verification Code")
					return
				}
				bindEmail(this.postData).then(res => {
					if(res.code == 0){
						uni.navigateTo({url:`/url:'/pages/login/success?tips=Your email address has been successfully bound&btnText=DONE&backUrl=/pages/more/setting',`})
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			}
		},
		
		components:{
			commonModal
		}
	};
</script>

<style scoped lang="scss">
	.ml-5 {
		margin-left: 10rpx;
	}

	.mt-27 {
		margin-top: 54rpx;
	}

	.page {
		padding:48rpx 32rpx 0 32rpx;
		background-color: #ffffff;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		height: 100%;

		.font {
			font-size: 40rpx;
			font-family: SF Pro Display;
			color: #000000;
		}

		.text_3 {
			font-weight: 700;
		}

		.text_4 {
			margin-top: 48rpx;
			color: #7f7f7f;
			font-size: 28rpx;
			font-family: SF Pro Display;
			letter-spacing: -0.28rpx;
			width: 524rpx;
		}

		.group {
			margin-top: 48rpx;
			border-bottom: 2rpx solid #1A7B99;
			.email-input{
				width: 100%;
				padding: 20rpx 0;
				margin-top: 24rpx;
			}
			.text_5 {
				color: #3e3e3e;
				letter-spacing: -0.32rpx;
			}
			.send-wrap{
				width: 100%;
				view{
					padding: 20rpx 40rpx;
					background: #1A7B99;
					border-radius: 5px 5px 5px 5px;
					font-size: 14px;
					font-weight: bold;
					color: #FFFFFF;
				}
				input{
					flex: 1;
				}
			}
		}
		.tips{
			margin-top: 24rpx;
			margin-bottom: 100rpx;
			font-weight: 400;
			font-size: 12px;
			color: #B0B1BF;
		}

		.text-wrapper_2 {
			padding: 20rpx 0;
			background-color: #1a7b99;
			border-radius: 10rpx;
			color: #ffffff;
			font-weight: 700;
			text-align: center;
		}

		.font_2 {
			font-size: 32rpx;
			font-family: SF Pro Display;
		}

		.section_2 {
			padding: 484rpx 0 720rpx;
			background-color: #1314164d;
			overflow: hidden;

			.section_4 {
				margin: 0 30rpx;
				padding: 32rpx 132rpx 32rpx 152rpx;
				background-color: #ffffff;
				border-radius: 20rpx;

				.image_6 {
					width: 110rpx;
					height: 110rpx;
				}
				.font_3 {
					font-size: 28rpx;
					font-family: SF Pro Display;
				}

				.text_7 {
					color: #7f7f7f;
				}

				.text-wrapper {
					padding: 16rpx 0;
					background-color: #ffffff33;
					border-radius: 30rpx;
					width: 188rpx;
					border: solid 2rpx #7f7f7f;

					.text_8 {
						color: #000000;
					}
				}
			}
		}
	}
</style>